package util.map;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @description: Map相关的知识学习
 * @author: Lidong
 * @time: 2020/7/9 22:23
 **/
public class MapTest {

    private static final Logger logger = LoggerFactory.getLogger(MapTest.class);

    /**
     * Map中找到最大和最小的数
     */
    @Test
    public void test1() {
        Map<String, String> map = new HashMap<>();
        map.put("1", "2");
        map.put("2", "4");
        map.put("3", "1");
        map.put("4", "5");
        map.put("5", "8");
        map.put("6", "9");
        map.put("7", "7");
        Collection<String> values = map.values();
        Object[] objects = values.toArray();
        Arrays.sort(objects);
        String min = String.valueOf(objects[0]);
        String max = objects[objects.length - 1].toString();
        logger.info("最大和最小值为：" + max + "====" + min);
    }


    /**
     * concurrentMap：有sigment数组，在获取某个Entery 元素的时候 首先需要获取 sigmen 锁
     */
    @Test
    public void testOne() {
        ConcurrentMap concurrentMap = new ConcurrentHashMap();
        concurrentMap.put("test", "张三");
        logger.info(String.valueOf(concurrentMap));
    }

    /**
     * Map 初始化
     */
    @Test
    public void initMap() {

        // Java 新特性初始化
        Map<String, String> mapOne = new HashMap<String, String>() {
            {
                put("one", "testOne");
                put("two", "testTwo");
                put("three", "testThree");
            }
        };
        logger.info(String.valueOf(mapOne));

        Map<String, String> mapTwo = new HashMap<>();
        mapTwo.put("a", "b");
        mapTwo.put("c", "d");
        logger.info(String.valueOf(mapTwo));

        ImmutableMap<String, Integer> map = ImmutableMap.of("a", 1, "b", 2, "c", 3);
        logger.info(String.valueOf(map));
    }

    /**
     * WeakHashMap
     */
    @Test
    public void testWeakHashMap() {
        WeakHashMap<String, String> map = new WeakHashMap<>();
        map.put("key_one", "test");
        map.put("key_two", "test");
        map.put("key_three", "test");
    }

    @Test
    public void linkedHashMapTest() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("key_one", "张三");
        map.put("key_two", "李四");
        map.put("key_three", "王五");
        logger.info(String.valueOf(map));
    }

    @Test
    public void testTwo() {
        Map<String, String> map = new HashMap<String, String>() {{
            put("one", "one");
            put("two", "one");
            put("three", "one");
        }};
        Map<String, String> mapOne = new HashMap<>();
        map.forEach(mapOne::put);
        logger.info(String.valueOf(mapOne));
    }

    /**
     * 遍历 Map
     */
    @Test
    public void testThree() {
        Map<String, String> map = new HashMap<String, String>() {{
            put("one", "1");
            put("two", "2");
        }};
        for (Map.Entry<String, String> entry : map.entrySet()) {
            logger.info("键：{}", entry.getKey());
            logger.info("值：{}", entry.getValue());
        }
        map.forEach(logger::info);
    }


    @Test
    public void testFour() {
        List<UpperKeyMap> list = new ArrayList<>();

        UpperKeyMap upperKeyMap = new UpperKeyMap();
        upperKeyMap.put("a", "1");
        upperKeyMap.put("b", "1");
        upperKeyMap.put("c", "1");
        list.add(upperKeyMap);
        for (UpperKeyMap map : list) {
            map.putOriginalKey('d', 4);
        }
        logger.info(list.toString());

    }

    @Test
    public void test2() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> view = (Map<String, Object>) map.get("test");
        logger.info(String.valueOf(view));
    }
}
